﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KingRoad.EPM.Web.Areas.PersonnelLog.ViewModels;
using KingRoad.EPM.Web.Areas.PersonnelLog.PersonnelLogFramework.Common;

namespace KingRoad.EPM.Web.Areas.PersonnelLog.Controllers
{
    public class PlmDirectorLogController : BaseController
    {
        /// <summary>
        /// 总监/驻地日志列表
        /// </summary>
        /// <returns></returns>
        public ActionResult DirectorLogList(string pageName)
        {
            var vm = new PlmDirectorlogViewModel();
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = 1;
            //每页多少条
            ViewBag.numPerPage = 20;
            var list = PlmDirectorlogUIRule.GetAllDirectorlogForPage(ViewBag.pageNum, ViewBag.numPerPage, "Dl_timestamp", out totalRecord, " Dl_ContractNumber in (" + Get_JL_JC_ZDJL_SG_SegmentCodes() + ") ");
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;


            vm.ThePlmDirectorlogList = list;
          //  return View(vm);
            return String.IsNullOrEmpty(pageName) ? View(vm) : View(pageName, vm);
        }

        /// <summary>
        /// 费用计划-列表(分页提交)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SplitPage()
        {
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = Convert.ToInt32(Request.Form["pageNum"]);
            //每页多少条
            ViewBag.numPerPage = Convert.ToInt32(Request.Form["numPerPage"]);
            if (ViewBag.pageNum == 0 || ViewBag.numPerPage == 0)
                return DirectorLogList("DirectorLogList");

            ViewBag.Dl_SupervisorName = Request.Form["Dl_SupervisorName"];
            ViewBag.Dl_SupervisionNumber = Request.Form["Dl_SupervisionNumber"];
            ViewBag.Dl_RecordDate = Request.Form["Dl_RecordDate"];
            ViewBag.Dl_ContractNumber = Request.Form["Dl_ContractNumber"];
            string sql = " Dl_ContractNumber in (" + Get_JL_JC_ZDJL_SG_SegmentCodes() + ") ";
            if (ViewBag.Dl_SupervisorName != "")
                sql += " and Dl_SupervisorName like '%" + ViewBag.Dl_SupervisorName + "%' ";
            if (ViewBag.Dl_SupervisionNumber != "")
                sql += " and Dl_SupervisionNumber like '%" + ViewBag.Dl_SupervisionNumber + "%' ";
            if (ViewBag.Dl_RecordDate != "")
                sql += " and Dl_RecordDate='" + ViewBag.Dl_RecordDate + "' ";
            if (ViewBag.Dl_ContractNumber != "")
                sql += " and Dl_ContractNumber like '%" + ViewBag.Dl_ContractNumber + "%' ";
            var List = PlmDirectorlogUIRule.GetAllDirectorlogForPage(ViewBag.pageNum, ViewBag.numPerPage, "Dl_timestamp", out totalRecord, sql);
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View("DirectorLogList", new PlmDirectorlogViewModel { ThePlmDirectorlogList = List });
        }
        /// <summary>
        /// 总监/驻地日志列表
        /// </summary>
        /// <returns></returns>
        public ActionResult DirectorLogAdd(string target, string id)
        {
            //获取模块ID
            ViewBag.MoudleID = GetEntityByUrl(Models.PageURL.总监_驻地日志);
            //附件目录名
            ViewBag.DirWhere = Models.DirName.总监_驻地日志;
            ViewBag.OperationType = target;
            if (target == "add")
            {
                var vm = new PlmDirectorlogViewModel();
                vm.ThePlmDirectorlog = new PersonnelLogEntity.Model.PlmDirectorlogInfo();
                vm.ThePlmDirectorlog.Dl_UserID = GetLoginUserId;
                vm.ThePlmDirectorlog.Dl_UserName = GetLoginUserRealName;
                vm.ThePlmDirectorlog.Dl_RecordDate = DateTime.Now;
                if (GetLoginUserOrganizeEntity.Code == "JL" && GetJLProjectSegmentInfo.Count > 0)
                    vm.ThePlmDirectorlog.Dl_ContractNumber = GetJLProjectSegmentInfo[0].SEGMENT_CODE;
                else
                {
                    if (GetZDJLProjectSegmentInfo.Count > 0)
                        vm.ThePlmDirectorlog.Dl_ContractNumber = GetZDJLProjectSegmentInfo[0].SEGMENT_CODE;
                }
                vm.ThePlmDirectorlog.Dl_RecordUser = GetLoginUserRealName;
                vm.ThePlmDirectorlog.Dl_SpecialtyPosition=GetLoginUserInfo.WorkgroupFullName;
                vm.ThePlmDirectorlog.Dl_SupervisorName = GetLoginUserDepartmentName;
                //附件
                var assisId = GenerateRandom();
                var assisIdRet = 0;
                CreateAssistId(PlmDirectorlogUIRule, assisId, out assisIdRet);
                ViewBag.AssisId = assisIdRet;
                return View(vm);
            }
            else
            {
                var signle = PlmDirectorlogUIRule.GetDirectorlogInfoById(id);
                var vm = new PlmDirectorlogViewModel { ThePlmDirectorlog = signle };
                //附件
                var commonFileList = CommonFileUIRule.GetCommonFilesByConditon(signle.Assist_ID, "DirectorLog_Rules");//DirectorLog_Rules  BizType类型
                ViewBag.AssisId = signle.Assist_ID;
                ViewBag.FileIds = string.Join(",", commonFileList.Select(t => t.FILE_FILEID));
                ViewBag.DeleteIds = string.Join(",", commonFileList.Select(t => t.FILE_ID));
                return View(vm);
            }
        }
        /// <summary>
        /// 总监/驻地日志列表
        /// </summary>
        /// <returns></returns>
        public ActionResult DirectorLogView(string target, string id)
        {
            //获取模块ID
            ViewBag.MoudleID = GetEntityByUrl(Models.PageURL.总监_驻地日志);
            //附件目录名
            ViewBag.DirWhere = Models.DirName.总监_驻地日志;
            var signle = PlmDirectorlogUIRule.GetDirectorlogInfoById(id);

            //附件
            var commonFileList = CommonFileUIRule.GetCommonFilesByConditon(signle.Assist_ID, "DirectorLog_Rules");//DirectorLog_Rules  BizType类型
            ViewBag.AssisId = signle.Assist_ID;
            ViewBag.FileIds = string.Join(",", commonFileList.Select(t => t.FILE_FILEID));
            ViewBag.DeleteIds = string.Join(",", commonFileList.Select(t => t.FILE_ID));

            var vm = new PlmDirectorlogViewModel { ThePlmDirectorlog = signle };
            return View(vm);
        }
        public ActionResult DirectorLogAddHandle(PlmDirectorlogViewModel vm)
        {
            if (vm == null)
            {
                return new JsonResult()
                {
                    Data = new
                    {
                        statusCode = "300",
                        message = "操作失败",
                    }
                };
            }
            dynamic jsonReturn = new ExpandoObject();
            string operation_str = string.Empty;
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                if (vm.OperationType == "add")
                {
                    operation_str = "添加";
                    var AssistID = Convert.ToInt32(Request.Form["AssistID"]);
                    vm.ThePlmDirectorlog.Assist_ID = AssistID;
                    vm.ThePlmDirectorlog.Dl_ID = Guid.NewGuid().ToString();
                    PlmDirectorlogUIRule.InsertEntity(vm.ThePlmDirectorlog);
                }
                if (vm.OperationType == "edit")
                {
                    operation_str = "更新";
                    var AssistID = Convert.ToInt32(Request.Form["AssistID"]);
                    vm.ThePlmDirectorlog.Assist_ID = AssistID;
                    PlmDirectorlogUIRule.UpdateEntity(vm.ThePlmDirectorlog);
                }
            });
            if (isScuess)
            {
                jsonReturn = new { statusCode = "200", message = string.Format("{0}成功", operation_str), navTabId = "PersonnelLog.PlmDirectorLog.DirectorLogList", rel = "", callbackType = "closeCurrent", forwardUrl = "", confirmMsg = "" };
            }
            else
            {
                jsonReturn = new { statusCode = "300", message = string.Format("{0}失败", operation_str) };
            }
            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        public ActionResult DirectorLogDelete(string id)
        {
            dynamic jsonReturn = new ExpandoObject();
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                PlmDirectorlogUIRule.DeleteEntity(id);
            });
            if (isScuess)
            {
                jsonReturn = new { statusCode = "200", message = "删除成功", navTabId = "PersonnelLog.PlmDirectorLog.DirectorLogList", rel = "", callbackType = "", forwardUrl = "", confirmMsg = "" };
            }
            else
            {
                jsonReturn = new { statusCode = "300", message = "删除失败" };
            }
            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        /// <summary>
        /// 检测日志导出
        /// </summary>
        /// <returns></returns>
        public FileResult DirectorLogExport(string id)
        {
            var signle = PlmDirectorlogUIRule.GetDirectorlogInfoById(id);
            string fileName = "总监与驻地监理日志.docx";
            return base.ExportToWord(fileName, signle);

        }

    }
}
